package 回溯法;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class 解数独简单 {
    static String[][] board = { {"1",".","."},
                                {".","2","."},
                                {"2",".","."}};
    static String[][] check = { {"1","0","0"},
                                {"0","1","0"},
                                {"1","0","0"}};
    static String[] number = {"1","2","3"};
    static List<Map<String,String>> listRow = new ArrayList<>(); // 行Map
    static List<Map<String,String>> listCloum = new ArrayList<>(); // 列Map
    public static void main(String[] args) {
        solveSudoku(board);
    }

    static public void solveSudoku(String[][] board) {
        int row = board.length;
        int cloum = board[0].length;
        listRow.clear();
        listCloum.clear();
        // 行 列
        for (int i = 0; i < row; i++) {
            Map<String,String> mapRow = new HashMap<>();
            Map<String,String> mapCloum = new HashMap<>();
            for (int j = 0; j < cloum; j++) {
                if(board[i][j] != "."){
                    mapRow.put(board[i][j],"1");
                }
                if (board[j][i] != "."){
                    mapCloum.put(board[j][i],"1");
                }
            }
            listRow.add(i,mapRow);
            listCloum.add(i,mapCloum);
        }
        System.out.println(listRow.size());
        System.out.println(listRow);
        System.out.println(listCloum.size());
        System.out.println(listCloum);

        fun(0,0);
        for (int i = 0; i < 3; i++) {
            for (int j = 0; j < 3; j++) {
                System.out.print(board[i][j]+"\t");
            }
            System.out.println();
        }

    }
    public static void fun(int h,int l){
        if(h == 3 && l == 3){
            return;
        }
        for (int i = h; i < 3; i++) {
            for (int j = l; j < 3; j++) {
                for (int k = 0; k < number.length; k++) {
                    if(check(number[k],i,j)){ // check[i][j]=="0" &&
                        board[i][j] = number[j];
                        // check[i][j] = "1";
                        listRow.get(i).put(number[k],"1");
                        listCloum.get(j).put(number[k],"1");
                        fun(i,j);
                        // board[i][j] = ".";
                        listRow.get(i).remove(number[k]);
                        listCloum.get(j).remove(number[k]);
                    }
                }

            }
        }
    }
    public static void fun1(int n){
        if(n == 3){
            return;
        }
        for (int l = 0; l < 3; l++) { // 控制列
            for (int j = 0; j < number.length; j++) {
                if(board[n][l] == "." && check(number[j],n,l)){
                    board[n][l] = number[j];
                    listRow.get(n).put(number[j],"1");
                    listCloum.get(l).put(number[j],"1");
                    fun1(n+1);
                    board[n][l] = ".";
                    listRow.get(n).remove(number[j]);
                    listCloum.get(l).remove(number[j]);
                }
            }
            if(l == n){
                fun1(n+1);
            }
        }
    }
    public static boolean check(String str,int i,int j){
        boolean isOk = false;
        if(null == listRow.get(i).get(str) && null == listRow.get(j).get(str)){
            isOk = true;
        }
        return isOk;
    }
}
